package com.ftg.learn.aop;

import com.ftg.learn.entity.LearnLog;
import com.ftg.learn.mapper.LearnLogMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 布衣
 */
@Aspect
@EnableAspectJAutoProxy
@Component
public class LogAop {

    @Resource
    LearnLogMapper llm;

    @Resource
    HttpServletRequest request;

    @Around("execution(public * com.ftg.learn.service.impl.*.*(..))")
    @Order(2)
    public Object sayAround(ProceedingJoinPoint pjp) {

        LearnLog ll = new LearnLog();

        Object obj = null; // 目标类里面的连接方法
        try {

            Long startTime = System.currentTimeMillis();
            obj = pjp.proceed();
            Long endTime = System.currentTimeMillis();
            ll.setOperTime((int) (endTime-startTime));
        } catch (Throwable e) {
            ll.setOperStatus(1);
            throw new RuntimeException(e);
        }finally{
            System.out.println("你的方法Around -- 结束");
        }

        ll.setMethodName(request.getMethod());
        ll.setModelName("模块名称");
        ll.setOperPersonName("解析token");
        ll.setOperAddress(request.getRemoteAddr());
        ll.setOperCity("IP所在地市");
        ll.setOperStatus(0);//0成功 1失败
        llm.insert(ll);
        return obj;
    }


}

